﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ServiceModel;
using System.Web.UI.WebControls;
using ProductServiceReference;

public partial class UserControls_EditProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AddProduct1.Controls.Remove(AddProduct1.FindControl("btnSubmit"));
        if (IsPostBack) return;
        var client = new ProductServiceClient();
        foreach (var product in client.GetProducts())
        {
            ddlProducts.Items.Add(new ListItem(product.ProductName, product.ProductID + ""));
        }
    }

    protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            var client = new ProductServiceClient();
            var product = client.GetProduct(int.Parse(ddlProducts.SelectedValue));
            ((TextBox) AddProduct1.FindControl("txtName")).Text = product.ProductName;
            ((TextBox) AddProduct1.FindControl("txtPrice")).Text = product.Price + "";
            ((TextBox) AddProduct1.FindControl("txtQuantity")).Text = product.Quantity + "";
            ((TextBox) AddProduct1.FindControl("txtOrigin")).Text = product.Origin + "";
            ((TextBox) AddProduct1.FindControl("txtGuaranteePeriod")).Text = product.GuaranteePeriod + "";
            ((DropDownList) AddProduct1.FindControl("ddlcat")).SelectedValue = product.CategoryID + "";
        }
        catch (FaultException<Product> faultException)
        {
            ((Label) AddProduct1.FindControl("msg")).Text = faultException.Message;
        }
        catch (Exception ex)
        {
            ((Label)AddProduct1.FindControl("msg")).Text = ex.Message;
        }
        UpdatePanel1.Update();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            var client = new ProductServiceClient();
            var result = client.UpdateProduct(new Product()
                                                  {
                                                      ProductName = ((TextBox)AddProduct1.FindControl("txtName")).Text,
                                                      Price =
                                                          decimal.Parse(
                                                              ((TextBox)AddProduct1.FindControl("txtPrice")).Text),
                                                      Quantity =
                                                          int.Parse(
                                                              ((TextBox)AddProduct1.FindControl("txtQuantity")).Text),
                                                      Origin = ((TextBox)AddProduct1.FindControl("txtOrigin")).Text,
                                                      GuaranteePeriod =
                                                          short.Parse(
                                                              ((TextBox)AddProduct1.FindControl("txtGuaranteePeriod")).
                                                                  Text),
                                                      CategoryID =
                                                          int.Parse(
                                                              ((DropDownList)AddProduct1.FindControl("ddlcat")).
                                                                  SelectedValue),
                                                      ProductImage =
                                                          ((FileUpload)AddProduct1.FindControl("upload")).HasFile
                                                              ? ((FileUpload)AddProduct1.FindControl("upload")).
                                                                    FileName
                                                              : "images/default.jpg",
                                                  }, int.Parse(ddlProducts.SelectedValue));


            ((Label)AddProduct1.FindControl("msg")).Text = result
                                                                ? "Edit product successfully!"
                                                                : "Edit product not success!";
        }
        catch (FaultException<Product> faultException)
        {
            ((Label) AddProduct1.FindControl("msg")).Text = faultException.Message;
        }
        catch (Exception ex)
        {
            ((Label)AddProduct1.FindControl("msg")).Text = ex.Message;
        }
     
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            var client = new ProductServiceClient();
            var result = client.DeleteProduct(int.Parse(ddlProducts.SelectedValue));
            ((Label)AddProduct1.FindControl("msg")).Text = result
                                                                ? "Delete product successfully!"
                                                                : "Delete product not success!";
        }
        catch (FaultException<Product> faultException)
        {
            ((Label)AddProduct1.FindControl("msg")).Text = faultException.Message;
        }
        catch (Exception ex)
        {
            ((Label)AddProduct1.FindControl("msg")).Text = ex.Message;
        }
        
    }
}